﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Show_Detail : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Attribute_Bind();
            Dep_Bind();
            Auth();
            SetPager();
        }
    }

    private void Auth()
    {
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            Dep_A.SelectedValue = dr["company"].ToString();
        }
        else
        {
            Dep_A.Enabled = true;
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds;
        if (Request.QueryString["xf"] != null && !IsPostBack)
        {
            SetPre();
        }
        ds = GetDetail();
        DetailPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = DetailPager.PageSize;
        pds.CurrentPageIndex = DetailPager.CurrentPageIndex - 1;
        DetailView.DataSource = pds;
        DetailView.DataBind();
    }

    private DataSet GetDetail()
    {
        string sqlstr = "AB_GetInfo_Pk.GetAlarm";
        OracleParameter[] _params = {
                new OracleParameter("V_Type",OracleType.VarChar,100),
                new OracleParameter("v_County",OracleType.VarChar,20),
                new OracleParameter("v_Cell",OracleType.VarChar,200),
				new OracleParameter("V_Begin",OracleType.VarChar,100),
				new OracleParameter("V_End",OracleType.VarChar,100),
				new OracleParameter("V_Net",OracleType.VarChar,20),
                new OracleParameter("V_IFLike",OracleType.VarChar,2),
                new OracleParameter("V_IFRevor",OracleType.VarChar,2),
				new OracleParameter("resultSet",OracleType.Cursor)
            };
        _params[0].Value = AlarmType.SelectedValue == "0" ? "" : AlarmType.SelectedValue;
        _params[1].Value = Dep_A.SelectedValue == "0" ? "" : Dep_A.SelectedValue;
        _params[2].Value = Cell_T.Text.Trim().Length == 0 ? "" : Tools.ReplaceString(Cell_T.Text.Trim(),100);
        _params[3].Value = Begin_Date.Text.Trim().Length > 0 ? Begin_Date.Text.Trim() : "";
        _params[4].Value = End_Date.Text.Trim().Length > 0 ? DateTime.Parse(End_Date.Text.Trim()).AddDays(1).ToShortDateString() : "";
        _params[5].Value = GType_A.SelectedValue == "0" ? "" : GType_A.SelectedValue;
        _params[6].Value = "Y";
        _params[7].Value = IFRecover.SelectedValue;
        _params[8].Direction = ParameterDirection.Output;
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.StoredProcedure, _params);
    }

    private void SetPre()
    {
        if (Request.QueryString["xf"] != null && !Request.QueryString["xf"].Equals("0"))
        {
            Dep_A.SelectedValue = Request.QueryString["xf"].ToString();
        }
        if (Request.QueryString["jz"] != null)
        {
            Cell_T.Text = Request.QueryString["jz"].ToString();
        }
        if (Request.QueryString["GType"] != null && !Request.QueryString["GType"].Equals("0"))
        {
            GType_A.SelectedValue = Request.QueryString["GType"].ToString();
        }
        if (Request.QueryString["BeginDate"] != null && Request.QueryString["BeginDate"].Trim().Length > 0)
        {
            Begin_Date.Text = Request.QueryString["BeginDate"].ToString();
        }
        if (Request.QueryString["EndDate"] != null && Request.QueryString["EndDate"].Trim().Length > 0)
        {
            End_Date.Text = Request.QueryString["EndDate"].ToString();
        }
        Return.Visible = true;
    }

    protected void Query_Click(object sender, EventArgs e)
    {
        DetailPager.CurrentPageIndex = 1;
        SetPager();
    }

    private void Attribute_Bind()
    {
        Begin_Date.Text = DateTime.Now.ToString("yyyy-MM-dd");
        Begin_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('BeginDate'),maxDate:'%y-%M-%d'});");
        End_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('EndDate'),maxDate:'%y-%M-%d'});");
    }

    private void Dep_Bind()
    {
        string sqlstr = "SELECT DISTINCT SUBSTR(Replace(CELL_CHNAME,'G',''),0,2) AS County FROM CELL_CLASS ORDER BY SUBSTR(Replace(CELL_CHNAME,'G',''),0,2)";
        Dep_A.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null).Tables[0].DefaultView;
        Dep_A.DataTextField = "County";
        Dep_A.DataValueField = "County";
        Dep_A.DataBind();
        Dep_A.Items.Insert(0, new ListItem("全市", "0"));
    }

    protected void DetailPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void DetailView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D5E5EA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }

    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("report.aspx?xf=" + Dep_A.SelectedValue + "&GType=" + GType_A.SelectedValue + "&BeginDate="
                + Begin_Date.Text + "&EndDate=" + End_Date.Text);
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void Excel_Click(object sender, EventArgs e)
    {
        DataSet ds = GetDetail();
        if (ds.Tables[0].Rows.Count > 65535)
        {
            JQueryAlert("告警明细总数已超过EXCEL最大限制65535行！\\n请缩小查询范围后再尝试导出！");
            return;
        }
        DetailView.DataSource = ds; 
        DetailView.AllowPaging = false;
        DetailView.DataBind();        
        Tools.DGToExcel(DetailView, Dep_A.Enabled ? "UTF-8" : "GB2312");
        SetPager();
    }
}
